﻿using System.Linq;

namespace ComeSeeThis_MVC2.Models
{
    public class CompanyAddressesRepository
    {
        private readonly ComeSeeThisDBEntities _entities = new ComeSeeThisDBEntities();

        #region Query Methods

        public IQueryable<CompanyAddress> GetAllAddresses()
        {
            return _entities.CompanyAddresses;
        }

        public CompanyAddress GetAddress(int companyAddressId)
        {
            return _entities.CompanyAddresses.SingleOrDefault(c => c.CompanyAddressId == companyAddressId);
        }

        public IQueryable<CompanyAddress> GetAddressesByCompanyId(int companyId)
        {
            return from a in _entities.CompanyAddresses
                   where a.CompanyId == companyId
                   orderby a.Address1
                   select a;
        }

        #endregion

        #region Insert / Delete Methods

        public void Add(CompanyAddress companyAddress)
        {
            _entities.CompanyAddresses.AddObject(companyAddress);
        }

        public void Delete(CompanyAddress companyAddress)
        {
            _entities.CompanyAddresses.DeleteObject(companyAddress);
        }

        #endregion

        #region Persistance

        public void Save()
        {
            _entities.SaveChanges();
        }

        #endregion

    }
}